<?php
namespace app\talbum\model;
use app\base\model\BaseModel;
use think\Db;
use think\facade\Log;
/**
 * 
 */
class Photo_album extends BaseModel
{
	//创建相册
	public function insert_album($data){
		try {
			Db::startTrans();
		    $albumid = Db::name('Photo_album')->insertGetId($data['album']);
		  	if(!empty($data['image'])){
		  		$data['image']['albumid']=$albumid;
		    	$picnumber = Db::name('photo_album_pics')->insert($data['image']);
		  	}
		    // 提交事务
		    Db::commit();
		    return 1;
		} catch (\Exception $e) {
		    // 回滚事务
		    Db::rollback();
		    return 0;
		}
		
	}
	//修改相册
	public function edit_album($data){

		try {
			Db::startTrans();

		    $albumid = Db::name('Photo_album')->update($data['album']);

		   	if(!empty($data['image'])){
		   		$up['Iscover']=0;
		   		Db::name('photo_album_pics')->where('albumid',$data['image']['albumid'])->update($up);
		   		$picnumber = Db::name('photo_album_pics')->insert($data['image']);
		   	}
		    // 提交事务
		    Db::commit();
		    return 1;
		} catch (\Exception $e) {
		    // 回滚事务
		    Db::rollback();

		    return 0;
		}
		
	}
	

//相册列表
	public function album_list($page=1,$limit,$where){
		$pre = ($page-1)*$limit;
		$data = Db::name('Photo_album p')->field('p.albumid,p.describe,p.Openid,p.albumname,p.Isuse,p.Hits,p.createtime,p.permissions,p.albumregion,(select pa.Picspath from xj_photo_album_pics as pa where pa.albumid = p.albumid ORDER BY Iscover desc limit 1) as cover')->leftJoin('photo_album_pics pa','p.albumid = pa.albumid')->where($where)->group('albumid')->limit($pre,$limit)->order('picsid','desc')->select();
	
		return $data;
	}
	//上传照片
	public function upload_photo($data){
		$picnumber = Db::name('photo_album_pics')->insertAll($data['image']);
		return $picnumber;
	}
	//查找图片
	public function find_photo($data){
		$where  = array('picsid' =>$data['picsid'],'Openid'=>$data["Openid"]);
		$res = $this->findByAttributes('photo_album_pics',$where,'Picspath');
		return $res;
	}
	public function find_photos($data){
		$where['picsid']  = array('in' =>$data['picsids']);
		$where['Openid']  =$data['Openid'];
		$res = $this->findAllByWhere('photo_album_pics',$where,'picsid,Picspath','picsid desc');
		return $res;
	}
	//删除单张图片
	public function del_album_photo($data){
		$up['status'] = 2;
		$where  = array('picsid' =>$data['picsid']);
		
		$comment = $this->updateByWhere('photo_picmsg',$where,$up);//删除评论
		$point = $this->updateByWhere('photo_point',$where,$up);//删除点赞
		$res = $this->updateByWhere('photo_album_pics',$where,$up);
		return $res;
	}


	//批量删除图片
	public function del_many_photos($delete){
		$up['status'] = 2;
		$where['picsid']  = array('in',$delete);
		$comment = $this->updateByWhere('photo_picmsg',$where,$up);//删除评论
		$point = $this->updateByWhere('photo_point',$where,$up);//删除点赞
		$res = $this->updateByWhere('photo_album_pics',$where,$up);
		return $res;
	}

	//删除相册
	public function del_albums($data){
		$up['status'] = 2;
		$where  = array('albumid' =>$data['albumid']);
		$comment = $this->updateByWhere('photo_picmsg',$where,$up);//删除评论
		$point = $this->updateByWhere('photo_point',$where,$up);//删除点赞
		$res = $this->updateByWhere('photo_album_pics',$where,$up);//删除图片
		$album = $this->updateByWhere('photo_album',$where,$up);
		return $album;
	}
	
	//上传时光相册和描述
	public function upload_photoa($data){
		$desc['describe']=$data['describe'];
		$desc['albumid']=$data['albumid'];
		$desc['Token']=$data['Token'];
		$desc['Hits']=0;
		$desc['Openid']=$data['Openid'];
		$desc['Isuse']=0;
		$desc['Userid']=$data['Userid'];
		$desc['createtime']=time(); 
		$pics=$data['image'];
		// 启动事务
		
		try {
			Db::startTrans();
		    $number = Db::name('photo_desc')->insertGetId($desc);
		   // log::write($number);
		    foreach ($pics as $key => $value) {
		    	$pics[$key]['number']=$number;
		    	
		    }
		    $picnumber = Db::name('photo_album_pics')->insertAll($pics);
		    // 提交事务
		    Db::commit();
		    return 1;
		} catch (\Exception $e) {
		    // 回滚事务
		    Db::rollback();
		    return 0;
		}
		
		
		
	}

	//get_count获得单张图片的评论和点赞数和图片地址
	public function get_count($data)
	{
		$res = Db::name('photo_album_pics')->alias('p')->field('p.Picspath,p.picsid,(select count(*) from xj_photo_picmsg as c where c.picsid = p.picsid) as comments,(select count(*) from xj_photo_point as po where po.picsid = p.picsid) as points')->where('p.picsid',$data['picsid'])->find();
		return $res;
	}

			//时光评论列表
	
	public function comment_list($data,$page=1,$limit=10){
		if(empty($data['limit'])) $limit=10;
			if(empty($data['page'])||$data['page']<=0) $page=1;
			$limit = $data['limit'];
			 $pre = ($page-1)*$limit;
			$res=Db::name('photo_picmsg')->alias('pm')->field('pm.cmsgid,pm.content,pm.createtime,u.wxnickname as name,r.wxnickname as replayname,u.UserImg as userimg,r.UserImg as replayimg')->join('userinfo u','u.Userid=pm.Userid','left')->join('userinfo r','pm.Replayid=r.Userid','left')->where('pm.picsid',$data['picsid'])->order('pm.createtime','desc')->limit($pre,$limit)->select();
			return $res;
		
	
	}



		//增加时光评论
	public function comment($data){
		$res = $this->insert('photo_picmsg',$data);
		return $res;
	}
}
?>